/*
    jME Terra package. A Terrain Engine for the Java Monkey Engine.
    Copyright (C) 2005-2006,  Tijl Houtbeckers
    
    **** LICENSE: ****

	This library is free software; you can redistribute it and/or
	modify it under the terms of the GNU Lesser General Public
	License as published by the Free Software Foundation,
	version 2.1.
	
	This library is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
	Lesser General Public License for more details.
	
	You should have received a copy of the GNU Lesser General Public
	License along with this library; if not, write to the Free Software
	Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

	**** CONTACT: ****

    I can be reached through the jmonkeyengine.com forums (user "llama"), 
    or the jmonkeyengine.com contact page:
    jmonkeyengine.com/index.php?option=com_contact&task=view&contact_id=5           

*/

/**
 * @author Tijl Houtbeckers
 */

package org.llama.jmex.terra.pass;

import java.nio.IntBuffer;
import java.util.WeakHashMap;

import org.llama.jmex.terra.SimpleTerraData;
import org.llama.jmex.terra.TerraData;
import org.llama.jmex.terra.TerraManager;
import org.llama.jmex.terra.TerraView;
import org.llama.jmex.terra.XYKey;
import org.llama.jmex.terra.util.LODUtils;

public class SimpleLODPass extends TerraViewPass {
	
	private long[] lods;
	private boolean flat;
	private IntBuffer[] indices;
	private Integer[] sequence;
	
	public SimpleLODPass(int blocksize, int[] levels, boolean flat) {
		lods = LODUtils.squareDistances(levels);
		this.flat = flat;
		sequence = LODUtils.sequence(levels.length);
		indices = LODUtils.generateIndicis(blocksize, levels.length);
		setKeyFilter(TerraManager.K_TD);

//		utils = new LODUtils()
		
	}
	
	public void addToView(TerraView view) {
		view.addPass(this, 1, TerraView.PASS_CURRENT);
		view.addPass(this, 2, TerraView.PASS_CURRENT);
		view.addPass(this, 0, TerraView.PASS_ADD);
	}
	
	private WeakHashMap lodMap = new WeakHashMap();	
	
	private void setMeshLevel(XYKey key, TerraView view) {
//System.out.println("setMeshLevel: "+key);
		long distsq = view.getTerraManager().distanceToBlockSquared(key.x, key.y, view.lastx, view.lasty);
		if (!flat) {
			distsq = distsq+(view.lasth * view.lasth);
		}
		
		int level = 0;
		while (level < lods.length && distsq > lods[level]) {
			level++;
		}				
		
		if (lodMap.get(key) != sequence[level]) {
			lodMap.put(key, sequence[level]);	
//			System.out.println("Set LOD level "+key+": "+sequence[level]);
		}
//System.out.println("setMeshLevel done: "+key);
	}
	
	private XYKey tempkey = new XYKey(0,0, TerraManager.K_TD);
	private int level(TerraView view, XYKey key, int level) {
		TerraData data = (TerraData) view.getTerraManager().get(key);
		if (data == null)
			return level;
		Integer inte = (Integer)lodMap.get(data.xy);
		if (inte == null)
			return level;
				
		return inte.intValue();
	}
	
	private void calculateIndicis(XYKey key, TerraView view) {
		int level = level(view, key, 0);
		
		boolean top = false, bottom = false, left = false, right = false;
		tempkey.update(key.x, key.y-1, TerraManager.K_TD);
		if (level(view, tempkey, level) > level)
			top = true;
		
		tempkey.update(key.x, key.y+1, TerraManager.K_TD);
		if (level(view, tempkey, level) > level)
			bottom = true;
		
		tempkey.update(key.x-1, key.y, TerraManager.K_TD);
		if (level(view, tempkey, level) > level)
			left = true;
		
		tempkey.update(key.x+1, key.y, TerraManager.K_TD);
		if (level(view, tempkey, level) > level)
			right = true;
		
		SimpleTerraData data = (SimpleTerraData) view.getTerraManager().get(key);
		IntBuffer index = LODUtils.getIndicis(level, indices, top, bottom, left, right);
		if (data.indicesbuffer != index) {
//			System.out.println("updated LOD:"+key+": "+level);
			data.indicesbuffer = index;
			data.updateMesh();
		}
	}
	

	public boolean pass(XYKey key, TerraView view, String passtype, int pass) {
		if (pass == 0) {
			setMeshLevel(key, view);
			calculateIndicis(key, view);
		}
		
		if (pass == 1) {
			setMeshLevel(key, view);
		}
		if (pass == 2) {
			calculateIndicis(key, view);
		}
		return false;
	}
	
	

}
